package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    //新增
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }

    //根据项分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询，基于mabatis的分页助手插件完成
        PageHelper.startPage(currentPage, pageSize);

        Page<CheckItem> page = checkItemMapper.selectByCondition(queryString);
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        return new PageResult(total, rows);
    }

    //通过id删除
    @Override
    public void deleteById(Integer id) {
        //判断检查项是否存在外键关联关系
        long countByCheckItemId = checkItemMapper.findCountByCheckItemId(id);

        if (countByCheckItemId > 0) {
            //存在关联，不允许删除
            throw new RuntimeException();
        }
        checkItemMapper.deleteById(id);
    }

    //编辑操作
    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }

    //检查组中检查检查项方法
    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.findAll();
    }


}
